package orest.front.collection;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import orest.back.userneeds.UserNeedsSql;
import orest.front.collection.CollectionSql;

import net.ssta.common.Constants;
import net.ssta.common.LoginUser;
import net.ssta.core.base.BaseException;
import net.ssta.core.base.ErrorCode;
import net.ssta.core.base.HandlerBase;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.DataBaseExecutor;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.web.RequestKit;
import net.ssta.core.util.DateUtility;


/**
 * 
 * @ClassName: MarketHandler
 * @Description: 需求市场相关处理
 * @author: zhanggk
 * @date: Feb 29, 2012
 * 
 */
public class CollectionHandler extends HandlerBase
{
    
    /**
     * 根据用户uuid获取该用户的收藏信息
     */
    public RecordSet getCollectionList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = CollectionSql.getCollectionByUseridSql();
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 根据用户uuid、收藏信息类别获取该用户的收藏信息
     */
    public RecordSet getCollectionClassList(RequestKit requestKit, PagingInfo pagingInfo)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            
            String columnid = requestKit.getParameter("columnid", "0");
            String sql = CollectionSql.getCollectionByClassUseridSql(columnid);
            recordSet = m_executor.find(sql, pagingInfo);
            return recordSet;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    /**
     * 添加收藏信息
     * 
     * @param url:收藏地址
     * @param title:信息标题
     * @param channelid：栏目id
     * @param infoid：信息id
     * **/
    
    
    /**
     * 删除收藏信息
     * @param uuid:用户收藏的uuid
     */
    public void collectionDelete(String collectionid)
    {
        Connection conn = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
        	//将指定的技术需求信息审批状态设置为审批通过状态
        	DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
        	String sql = CollectionSql.deleteCollectionSql(collectionid);
        	m_executor.execute(sql);
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    /**
     * 根据uuid获取用户收藏的详细信息
     */
    public Record getCollectionById(String uuid)
    {
        Connection conn = null;
        RecordSet recordSet = null;
        Record record = null;
        try
        {
            conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
            DataBaseExecutor m_executor = DataBaseExecutor.getExecutor(conn);
            String sql = CollectionSql.getCollectionInfoSql(uuid);
            recordSet = m_executor.find(sql);
            if(recordSet!=null && recordSet.size()>0){
            	record = recordSet.get(0);
            }
            
            return record;
        }
        catch (SQLException e)
        {
            throw new BaseException(ErrorCode.DB_EXCEPTION, e);
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    throw new BaseException(ErrorCode.CON_NOT_CLOSE, e);
                }
            }
        }
    }
    
    
    
}

